package paperfly.game.state;

import java.io.File;
import java.io.IOException;

import paperfly.game.PaperFlyGame;
import paperfly.game.Player;
import paperfly.game.handler.MenuHandler;

import com.jme.image.Texture;
import com.jme.input.KeyBindingManager;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.game.state.CameraGameState;
import com.jmex.game.state.GameStateManager;

public class MainMenuState extends CameraGameState {
	private static final String[] msg = { "Sair", "Opcoes", "Iniciar" };

	private static final int INICIAR = 2;
	private static final int OPCOES = 1;
	private static final int SAIR = 0;
	private Text[] textos;
	private Text selectedText;
	private Text lastSelectedText;
	private MenuHandler input;

	private Quad background;

	public MainMenuState(String string) {
		super(string);
		PaperFlyGame.recriar();
		DisplaySystem display = DisplaySystem.getDisplaySystem();
		background = new Quad("Background", display.getWidth(), display
				.getHeight());
		background.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		background.setColorBuffer(0, null);
		background.setLocalTranslation(new Vector3f(display.getWidth() / 2,
				display.getHeight() / 2, 0.0f));

		TextureState texState = display.getRenderer().createTextureState();
		Texture tex = TextureManager.loadTexture("images/loading/1.gif",
				Texture.MM_LINEAR, Texture.FM_LINEAR);
		texState.setTexture(tex);
		background.setRenderState(texState);

		background.updateRenderState();

		background.updateRenderState();
		rootNode.attachChildAt(background, 0);

		textos = new Text[3];
		textos[INICIAR] = Text.createDefaultTextLabel("inicio");
		textos[OPCOES] = Text.createDefaultTextLabel("opcoes");
		textos[SAIR] = Text.createDefaultTextLabel("sair");

		selectedText = textos[INICIAR];

		for (int i = SAIR; i < textos.length; i++) {
			textos[i].updateRenderState();
			textos[i].print(msg[i]);
			textos[i].getLocalTranslation().set(100, 100 * (i + 1), 0);
		}

		rootNode.attachChild(textos[INICIAR]);
		rootNode.attachChild(textos[OPCOES]);
		rootNode.attachChild(textos[SAIR]);

		rootNode.setLightCombineMode(LightState.OFF);
		rootNode.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		rootNode.updateRenderState();
		rootNode.updateGeometricState(0, true);
		input = new MenuHandler();

	}

	@Override
	protected void stateRender(float tpf) {
		selectedText.updateRenderState();
		try {
			Thread.sleep(80);
		} catch (InterruptedException e) {
		}
		super.stateRender(tpf);
	}

	@Override
	protected void stateUpdate(float tpf) {
		KeyBindingManager teclado = KeyBindingManager.getKeyBindingManager();
		if (teclado.isValidCommand(MenuHandler.COMANDO_BAIXO)) {
			int opcao = getIndiceSelecionado();
			lastSelectedText = selectedText;
			if (opcao > SAIR) {
				selectedText = textos[opcao - 1];
			} else
				selectedText = textos[INICIAR];
		}
		if (teclado.isValidCommand(MenuHandler.COMANDO_ALTO)) {
			int opcao = getIndiceSelecionado();
			lastSelectedText = selectedText;
			if (opcao < INICIAR) {
				selectedText = textos[opcao + 1];
			} else
				selectedText = textos[SAIR];
		}
		if (teclado.isValidCommand(MenuHandler.COMANDO_SELECIONAR)) {
			if (getIndiceSelecionado() == SAIR) {
				sair();
			}
			if (getIndiceSelecionado() == INICIAR) {
				iniciar();
			} else {
				showOptions();
			}
		}
		selectedText.setLocalScale(2);
		if (lastSelectedText != null) {
			lastSelectedText.setLocalScale(1);
		}
		super.stateUpdate(tpf);
	}

	private void showOptions() {
		OptionsMenuState options = new OptionsMenuState("options", this);
		GameStateManager.getInstance().attachChild(options);
		options.setActive(true);
		setActive(false);
	}

	protected void onActivate() {
		// input.inserirComandos();
		super.onActivate();
	}

	protected void onDeactivate() {
		// input.removerComandos();
		super.onDeactivate();
	}

	private void iniciar() {
		try {
			Savable playerModel = BinaryImporter.getInstance().load(new File("models/jme/f18.jme"));
			Player player = new Player("f18", (Spatial) playerModel, 50, 0, 100, 100, 1, 25);
			EnterGameState state = new EnterGameState(player);
			GameStateManager.getInstance().attachChild(state);
			setActive(false);
			state.setActive(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sair() {
		PaperFlyGame.sair();
	}

	private int getIndiceSelecionado() {
		for (int i = 0; i < textos.length; i++) {
			if (selectedText == textos[i])
				return i;
		}
		return 0;
	}
}
